import React, { useRef } from 'react'
import { useSelector } from 'react-redux'
import styled from 'styled-components'
import { RootState } from '../store'

const ModalDiv = styled.div`
    display: none;
    position: fixed;
    z-index: 1;
    padding-top: 200px;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0,0.4);

`
const ModalContent = styled.div`
    background-color: #fefefe;
    margin: auto;
    padding: 0 20px;
    border: 1px solid #888;
    width: 800px;
`
const CloseBtn = styled.div`
    
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    width: 100%;
    height: 40px;
    color: #aaaaaa;
    font-size: 20px;
    font-weight: bold;
    &:hover, &:focus{
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }
`

const Modal = (props) => {

    useSelector((state: RootState) => {
        if(state.editor.idx === "popmodal"){
            
            if(myModal) myModal.current.style.display = 'block';
        }
    });

    const myModal = useRef(null);

    const btnclick = () => {
        myModal.current.style.display = 'none';
    }

    return (
        <>
            <ModalDiv ref={myModal}>
                <ModalContent>
                    <CloseBtn onClick={btnclick}>
                        <span>{props.title}</span> 
                        <span>&times;</span>
                    </CloseBtn>
                    {props.children}
                </ModalContent>
            </ModalDiv>
        </>
    )
}

export default Modal